<?php
/*
 * code to display datagrid of users through an AJAX request
*/
//file to generate table rows of user data
// Create connection
include("../inc_files/utils/dbconnection.php");
//query for all customers
$query ="SELECT staff.FirstName, staff.LastName,
		staff.Password, roles.Role, staff.UserID, staff.Disabled
		FROM staff INNER JOIN roles ON  staff.Role = roles.RoleID
		WHERE staff.Deleted != 1";
$result = $mysqli->query( $query );

//count the number of returned results
$resultCount = $result->num_rows;

//if there are some results returned create a table showing the results
if( $resultCount > 0){
	$rownumber = 0;

	echo "<table class='datagrid'>
			<tr class=\"datagrid headerrow\">

			<th style=\"width:25%\">UserID</th>
			<th style=\"width:40%\">Name</th>
			<th style=\"width:20%\">role</th>
			<th style=\"width:10%\">Disabled</th>
			<th style=\"width:5%\">Edit</th>
			<th style=\"width:5%\">Delete</th>
			</tr>";

	while($row = mysqli_fetch_array($result))
	{

		if($rownumber %2 == 0)
		{
			echo "<tr class=\"datagrid evenrow\">";
		}
		else
		{
			echo "<tr class=\"datagrid oddrow\">";
		}
		$fullName = $row['FirstName'] ." ". $row['LastName'];
		echo "<td style=\"width:25%\">" . $row['UserID'] . "</td>";
		echo "<td style=\"width:40%\">" . $fullName . "</td>";
		echo "<td style=\"width:20%\">" . $row['Role'] . "</td>";
		$disabled="";
		$disabled = ($row['Disabled']==1)?"Yes" : "No";
		echo "<td style=\"width:10%\">" . $disabled . "</td>";
		echo "<td style=\"width:5%\"><input type=\"image\"  height=\"32\" width=\"32\"  src=\"../images/pencil_32.png\" alt=\"Delete records for ". $fullName ."\" onclick=\"window.location.href='edituser.php?userid="  . $row['UserID'] . "'\"></td>";
		echo "<td style=\"width:5%\"><img height=\"32\" width=\"32\"  src=\"../images/edit-delete-2-32.png\" alt=\"Delete records for ". $fullName ."\" onclick=\"deleteuser('"  . $row['UserID'] . "')\" style=\"cursor:pointer\"></td>";
		echo "</tr>";
		$rownumber++;
	}
	echo "<tr class=\"datagrid footer\">
			<td colspan=\"4\" style=\"width:80%\"><a href=\"/f21na/admin/newuser.php\">Add new user
			<img src=\"../images/add_32.png\" style=\"border:none\" height=\"32\" width=\"32\" alt=\" Add new user.\" ></a>
			</td>
			<td colspan=\"2\" style=\"width:20%\"></td>
			</tr>";
	echo "</table>";
}
//if no results returned say so.
else
{

	echo"No records returned";
}
//tidy up database connection
$mysqli->close();


?>